package com.sskj.run.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.run.bean.CoinInfo;
import com.sskj.run.bean.CoinTypeBean;
import com.sskj.run.bean.CommonBean;
import com.sskj.run.bean.OpenAwardBean;
import com.sskj.run.http.HttpConfig;
import com.sskj.run.ui.fragment.QiangjinFragment;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.functions.BiFunction;


public class QiangjinFragmentPresenter extends BasePresenter<QiangjinFragment> {


    public void getData() {
        Flowable<List<CoinTypeBean>> coinTypeFlow = httpService.getCoinType()
                .converter(new JsonConverter<HttpData<List<CoinTypeBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData());
        Flowable<CommonBean.BuyInfoBean> commonFlow = httpService.getCommonData()
                .converter(new JsonConverter<HttpData<CommonBean>>() {
                })
                .adapt(new FlowableBody<>())
                .map(commonBeanHttpData -> commonBeanHttpData.getData().getBuy_info());
        Flowable.zip(coinTypeFlow, commonFlow, (listHttpData, commonBeanHttpData) -> {

            ArrayList<CoinInfo> coinInfos = new ArrayList<>();
            Flowable.fromIterable(listHttpData)
                    .filter(coinTypeBean -> coinTypeBean.getIs_game() == 1)
                    .subscribe(coinTypeBean -> {
                        if (coinTypeBean.getCode().equals("USDT")) {
                            if (commonBeanHttpData.getUSDT_info() != null) {
                                commonBeanHttpData.getUSDT_info().setType("USDT");
                                coinInfos.add(commonBeanHttpData.getUSDT_info());
                            }
                        }
                        if (coinTypeBean.getCode().equals("CNB")) {
                            if (commonBeanHttpData.getCNB_info() != null) {
                                commonBeanHttpData.getCNB_info().setType("CNB");
                                coinInfos.add(commonBeanHttpData.getCNB_info());
                            }
                        }
                        if (coinTypeBean.getCode().equals("SOP")) {
                            if (commonBeanHttpData.getSOP_info() != null) {
                                commonBeanHttpData.getSOP_info().setType("SOP");
                                coinInfos.add(commonBeanHttpData.getSOP_info());
                            }
                        }
                        if (coinTypeBean.getCode().equals("LAC")) {
                            if (commonBeanHttpData.getLAC_info() != null) {
                                commonBeanHttpData.getLAC_info().setType("LAC");
                                coinInfos.add(commonBeanHttpData.getLAC_info());
                            }
                        }
                    });
            return coinInfos;
        })
                .subscribe(coinInfos -> {
                    mView.updataUI(coinInfos);
                }, e -> {
                    System.out.println(e);
                });
    }


    /**
     * 普通用户下注
     */
    public void normalBet(String type, String money, String escape_beishu) {
        httpService.normalBet(type, money, escape_beishu)
                .tag(this)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            ToastUtil.showShort(httpData.msg);
                            mView.betSuccess(type, money, escape_beishu);
                        } else {
                            ToastUtil.showShort(httpData.msg);
                            mView.betFail();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                        mView.betFail();
                    }
                });
    }


    /**
     * 普通用户逃跑
     */
    public void normalRun() {
        httpService.normalRun()
                .tag(this)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.msg);
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.runSuccess();

                        }
                    }
                });
    }





}
